<template>
	<view class="content">
		<view class="input-group">
			<view class="input-row border">
				<text class="title">账号：</text>
				<m-input type="text" focus clearable v-model="account" placeholder="请输入账号"></m-input>
			</view>
			<view class="input-row border">
				<text class="title">密码：</text>
				<m-input type="password" displayable v-model="password" placeholder="请输入密码"></m-input>
			</view>
			<view class="input-row border">
				<text class="title">密码：</text>
				<m-input type="password" displayable v-model="pwd" placeholder="请确认密码"></m-input>
			</view>
			<!-- <view class="input-row">
				<text class="title">邮箱：</text>
				<m-input type="text" clearable v-model="email" placeholder="请输入邮箱"></m-input>
			</view> -->
			<view class="input-row border">
				<text class="title">电话：</text>
				<input  class="tel" type="number" clearable v-model="tel" placeholder="请输入电话号码" @blur="checkTel"></input>
			</view>
			<view class="input-row border checkcode">
				<text>验证码：</text>
				<m-input type="text" focus clearable v-model="code" placeholder="请输入验证码"></m-input>
				<button @click="checkcode">发送验证码</button>
			</view>
		</view>
		<view class="btn-row">
			<button type="primary" class="primary" @tap="register">注册</button>
		</view>
	</view>
</template>

<script>
	import service from '../service.js';
	import mInput from '@/components/m-input.vue';
	 // import "components/m-icon/m-icon.css";
	export default {
		components: {
			mInput
		},
		data() {
			return {
				tel:'',
				account: '',
				password: '',
				pwd:'',
				email: '',
				//flag=true说明号码重复
				flag:true,
				code:'',
				code1:'1'
			}
		},
		methods: {
			register() {
				/**
				 * 客户端对账号信息进行一些必要的校验。
				 * 实际开发中，根据业务需要进行处理，这里仅做示例。
				 */
				if (this.tel.length!=11 || !this.tel.startsWith('1')) {
					uni.showToast({
						icon: 'none',
						title: '电话号码格式有误'
					});
					return;
				}
				//电话重复直接return
				if(this.flag){
					return
				}
				if (this.account.length < 5) {
					uni.showToast({
						icon: 'none',
						title: '账号最短为 5 个字符'
					});
					return;
				}
				if (this.password.length < 6) {
					uni.showToast({
						icon: 'none',
						title: '密码最短为 6 个字符'
					});
					return;
				}
				if (this.pwd != this.password) {
					uni.showToast({
						icon: 'none',
						title: '两次密码不一致'
					});
					return;
				}
				if(this.code!=this.code1){
					uni.showToast({
						icon: 'none',
						title: '验证码错误'
					});
					return;
				}
				this.reg()
			},
			//注册的方法
			reg(){
				uni.request({
					url: this.webUrl+'user/register?username='+this.account+'&password=' +this.password+'&tel='+this.tel,
					success: (res) => {
						if(res.data.code=='200'){
							uni.showToast({
								title: '注册成功'
							})
							uni.setStorageSync('user',res.data.obj)
							var user = uni.getStorageSync('user')
							console.log(user)
							// uni.setStorage({
							// 	key:"user",
							// 	data:res.data.obj
							// })
							// uni.getStorage({
							// 	key:"user",
							// 	success(res){
							// 		console.log(res)
							// 	}
							// })
							//登陆成功 17783700194
							// this.toMain(res.data.obj.userName);
							this.gotomine()
						}else{
							uni.showToast({
								title:'注册失败'
							})
						}
					}
				})
			},
			gotomine(){
				uni.reLaunch({
					url:"/pages/user/user"
				});
			},
			// 检查电话是否已存在
			checkTel(){
				uni.request({
					url: this.webUrl+'user/checkTel?tel='+this.tel,
					success: (res) => {
						if(res.data.code=='200'){
							uni.showToast({
								title: '电话号码已存在'
							})
						}else{
							this.flag = false;
						}
					}
				})
			},
			// 验证码
			checkcode(){
				if (this.tel.length!=11 || !this.tel.startsWith('1')) {
					uni.showToast({
						icon: 'none',
						title: '电话号码格式有误'
					});
					return;
				}
				this.checkTel();
				if(this.flag){
					return;
				}
				uni.request({
					url: this.webUrl+'/user/sendSms?tel='+this.tel,
					success: (res) => {
						if(res.data.code=='200'){
							uni.showToast({
								title: '已发送验证码'
							})
							this.code1=res.data.message;
							console.log(this.code1)
						}else{
							uni.showToast({
								title: '验证码发送失败'
							})
						}
					}
				})
			}
		}
	}
</script>

<style>
	.tel{
		padding-top: 10rpx;
		margin-top: 10rpx;
		margin-left: 18rpx;
	}
	.checkcode{
		margin-left: 30rpx;
	}
/*每个页面公共css */
	page {
		min-height: 100%;
		display: flex;
		font-size: 16px;
	}
	
	/* #ifdef MP-BAIDU */
	page {
		width: 100%;
		height: 100%;
		display: block;
	}
	
	swan-template {
		width: 100%;
		min-height: 100%;
		display: flex;
	}
	
	/* 原生组件模式下需要注意组件外部样式 */
	custom-component {
		width: 100%;
		min-height: 100%;
		display: flex;
	}
	
	/* #endif */
	
	/* #ifdef MP-ALIPAY */
	page {
		min-height: 100vh;
	}
	
	/* #endif */
	
	/* 原生组件模式下需要注意组件外部样式 */
	m-input {
		width: 100%;
		/* min-height: 100%; */
		display: flex;
		flex: 1;
	}
	input{
		width: 100%;
		/* min-height: 100%; */
		display: flex;
		flex: 1;
	}
	.content {
		display: flex;
		flex: 1;
		flex-direction: column;
		background-color: #efeff4;
		padding: 10px;
	}
	
	.input-group {
		background-color: #ffffff;
		margin-top: 20px;
		position: relative;
	}
	
	.input-group::before {
		position: absolute;
		right: 0;
		top: 0;
		left: 0;
		height: 1px;
		content: '';
		-webkit-transform: scaleY(.5);
		transform: scaleY(.5);
		background-color: #c8c7cc;
	}
	
	.input-group::after {
		position: absolute;
		right: 0;
		bottom: 0;
		left: 0;
		height: 1px;
		content: '';
		-webkit-transform: scaleY(.5);
		transform: scaleY(.5);
		background-color: #c8c7cc;
	}
	
	.input-row {
		display: flex;
		flex-direction: row;
		position: relative;
		font-size: 18px;
		line-height: 40px;
	}
	
	.input-row .title {
		width: 72px;
		padding-left: 15px;
	}
	
	.input-row.border::after {
		position: absolute;
		right: 0;
		bottom: 0;
		left: 8px;
		height: 1px;
		content: '';
		-webkit-transform: scaleY(.5);
		transform: scaleY(.5);
		background-color: #c8c7cc;
	}
	
	.btn-row {
		margin-top: 25px;
		padding: 10px;
	}
	
	button.primary {
		background-color: #0faeff;
	}
</style>
